<#import "/WEB-INF/sys/js_css.html" as js>
<#import "macro_archives.html" as archives>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文档管理</title>
</head>
<body>
	<!-- 工具条 -->
	<div id="tools" style="padding:5px;height:auto">
		<a href="javascript:void(0)" id="btn_top_add" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加文档</a>
		<a href="javascript:void(0)" id="btn_top_search" class="easyui-linkbutton" iconCls="icon-search" plain="true">条件过滤</a>
	</div>
	<table id="datagrid" class="easyui-datagrid" data-options="singleSelect:true,fit:true,fitColumns:true,pagination:true,
		url:'${ctx}/sys/archives/search',toolbar:'#tools',rownumbers : true,pageSize:15,pageList:[5,10,15,20]">
	    <thead>
			<tr>
				<th data-options="field:'id',hidden:true">id</th><!-- hidden -->
				<th data-options="field:'action',align:'center',width:10,formatter:formatterAction">操作</th>
				<th data-options="field:'title',width:40,align:'center'">标题</th>
				<th data-options="field:'colname',align:'center',width:20">所属栏目</th>
				<th data-options="field:'author',align:'center',width:10">作者</th>
				<th data-options="field:'noteusername',align:'center',width:10">录入人</th>
				<th data-options="field:'createTime',align:'center',width:10">创建时间</th>
			</tr>
		</thead>
    </table>
    <@archives.content />
</body>
</html>
<@js.easyui />
<@js.zTree />
<link type="text/css" rel="stylesheet" href="${ctx}/js/zTree/css/demo.css" type="text/css">
<link rel="stylesheet" href="${ctx}/js/kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="${ctx}/js/kindeditor/plugins/code/prettify.css" />
<script type="text/javascript" charset="utf-8" src="${ctx}/js/kindeditor/kindeditor.js"></script>
<script type="text/javascript" charset="utf-8" src="${ctx}/js/kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript" charset="utf-8" src="${ctx}/js/kindeditor/plugins/code/prettify.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
	//URL集盒合确定按钮事件
	jQuery("#btn_add_url_ok").click(function(){
		var value = "";
		jQuery.each(jQuery(".urlsingletext"),function(i,n){
			value = value+jQuery(n).val()+"§";
		});
		if (value.length > 0 ) value = value.substring(0, value.length-1);
		jQuery("#addurlist").val(value);
		jQuery("#add_window_urllist").window("close");
	});
	//添加url集合的按钮事件
	jQuery("#btn_add_window_urlist").click(function(){
		jQuery("#add_window_urllist").window("open");
		jQuery("#ul_urlist").empty();//清理掉里面现有的内容
		var value = jQuery("#addurlist").val();
		if(value==""){
			jQuery("#ul_urlist").append("<li><input type='text' size='45' class='urlsingletext' value='' /><input type='button' value='添加下一行' onclick='addurlsingletext()' /><input type='button' value='删除本行' onclick='deleteurlsingletext(this)' /></li>");//添加一个url文本信息
			return;
		}
		var strs = value.split("§");
		for(var i=0;i<strs.length;i++){
			jQuery("#ul_urlist").append("<li><input type='text' size='45' class='urlsingletext' value='"+strs[i]+"' /><input type='button' value='添加下一行' onclick='addurlsingletext()' /><input type='button' value='删除本行' onclick='deleteurlsingletext(this)' /></li>");//添加一个url文本信息
		}
	});
	//添加多个图片窗口单个图片的添加按钮事件
	jQuery("#btn_add_single_image").click(function(){
		jQuery("#add_window_list_microimg_upload").window("open");
		jQuery("#microimg_list_upload_form").form("clear");
	});
	//添加图片集合按钮事件
	jQuery("#btn_add_imagelist").click(function(){
		jQuery("#ul_image").empty();
		jQuery("#add_window_imagelist").window("open");
		var value = jQuery("#addimglist").val();
		if(value=="")
			return;
		var strs = value.split("§");
		for(var i=0;i<strs.length;i++){
			var str = strs[i];
			var srcs = str.split("※");
			jQuery("#ul_image").append("<li style='margin-top:5px;float:left;'><div style='float:left'><input type='checkbox' class='imageid' style='z-index:100px; margin-top:0px; position:relative' value='"+str+"' /><img style='float:left' src='"+srcs[2]+"' width='150px' height='150px' /></div></li>");
		}
	});
	//删除图片集合按钮事件
	jQuery("#btn_delete_single_image").click(function(){
		jQuery.each(jQuery(".imageid"),function(i,n){
			if(n.checked){
				var div = jQuery(n).parent().parent();
				jQuery(div).remove();
			}
		});
	});
	//确定图片集合按钮事件
	jQuery("#btn_add_single_image_ok").click(function(){
		var value = "";
		jQuery.each(jQuery(".imageid"),function(i,n){
			value = value+jQuery(n).val()+"§";
		});
		if (value.length > 0 ) value = value.substring(0, value.length-1);
		jQuery("#addimglist").val(value);
		jQuery("#add_window_imagelist").window("close");
	});
	//缩略图上传按钮事件
	jQuery("#btn_microimg_upload").click(function(){
		jQuery("#add_window_microimg_upload").window("open");
		jQuery("#microimg_upload_form").form("clear");
	});
	//添加文档按钮事件
	jQuery("#btn_top_add").click(function(){
		jQuery("#divpadd").window("open");
		jQuery("#topaddform").form("clear");
		jQuery("#img_show").attr("src", "");
		jQuery("#htmlcontent").val("");
		jQuery("#addtitle").attr("readonly",false);
		jQuery("#addshottitle").attr("readonly",false);
		editor.html("");
		editor.text("");
	});
	//缩略图确认上传按钮事件
	jQuery("#microimg_btn_upload").click(function(){
		//上传事件验证
		if(jQuery("#microimg").val()==""){
			jQuery.messager.alert("信息","请选择要上传的缩略图！","info");
			return;
		}
		if(!microimgfilter()){
			return;
		}
		jQuery("#microimg_upload_form").form("submit",{
			success:function(data){
				var data = eval("(" + data + ")");
				if(data.error<1){
					jQuery.messager.alert("信息","上传成功！","info",function(){
						jQuery("#img_show").attr("src", data.url);
						jQuery("#microimgid").val(data.id);
					});
				}else{
					jQuery.messager.alert("信息","上传失败！","error");
				}
				jQuery("#add_window_microimg_upload").window("close");
			}
		});
	});
	//图片集合单个确认上传按钮事件
	jQuery("#list_microimg_btn_upload").click(function(){
		//上传事件验证
		if(jQuery("#list_microimg").val()==""){
			jQuery.messager.alert("信息","请选择要上传的缩略图！","info");
			return;
		}
		if(!listimgfilter()){
			return;
		}
		jQuery("#microimg_list_upload_form").form("submit",{
			success:function(data){
				var data = eval("(" + data + ")");
				if(data.error<1){
					jQuery.messager.alert("信息","上传成功！","info",function(){
						var desc = jQuery("#addlist_img_desc").val();
						var src = data.url;
						var id = data.id;
						jQuery("#ul_image").append("<li style='margin-top:5px;float:left;'><div style='float:left'><input type='checkbox' class='imageid' style='z-index:100px; margin-top:0px; position:relative' value='"+id+"※"+desc+"※"+src+"' /><img style='float:left' src='"+src+"' width='150px' height='150px' /></div></li>");
					});
				}else{
					jQuery.messager.alert("信息","上传失败！","error");
				}
				jQuery("#add_window_list_microimg_upload").window("close");
			}
		});
	});
	//文章保存按钮事件
	jQuery("#btn_window_top_add").click(function(){
		//标题验证
		if(jQuery("#addtitle").val()==""){
			jQuery.messager.alert("信息","请填写文章标题！","info");
			return;
		}
		//文件上栏目验证
		if(jQuery("#zhucolnameid").val()==""){
			jQuery.messager.alert("信息","请选择主栏目！","info");
			return;
		}
		jQuery("#addtextcontent").val(editor.text());
		jQuery("#htmlcontent").val(editor.html());
		var ajaxvali = false;
		//ajax验证
		jQuery.ajax({
   			async:false,
   			type:"post",
   			dataType:"json",
   			data:{title:jQuery("#addtitle").val(),id:jQuery("#addid").val()},
   			url:"${ctx}/sys/archives/validate",
   			success:function(msg){
   				if(!msg.success){
   					jQuery.messager.alert("信息","已经存在名为"+jQuery("#addtitle").val()+"的文章！","error");
   					ajaxvali = true;
   					return;
   				}
   			}
   		});
		if(ajaxvali)
			return;

		//文章添加按钮提交
		jQuery("#topaddform").form("submit",{
			success:function(data){
				var data = eval("(" + data + ")");
				if(data.success){
					jQuery.messager.alert("信息","保存文章成功！","info",function(){
						jQuery("#datagrid").datagrid("reload");
						jQuery("#divpadd").window("close");
					});
				}else{
					jQuery.messager.alert("信息","保存文章失败！","error");
				}
			}
		});
	});
});
//显示操作里的内容
function formatterAction(value,row,index){
	return "<img title='修改此栏目' onclick='editfun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/pencil.png' />&nbsp;&nbsp;"+
	"<img title='删除此栏目' onclick='deletefun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/edit_remove.png' />";
}
//修改栏目信息
function editfun(id){
	jQuery.ajax({
		async:false,
		type:"post",
		dataType:"json",
		data:{id:id},
		url:"${ctx}/sys/archives/bean",
		success:function(date){
			if(!date.success){
				jQuery.messager.alert("信息",date.message,"error");
				return;
			}
			//先清理信息
			jQuery("#divpadd").window("open");
			jQuery("#topaddform").form("clear");
			jQuery("#img_show").attr("src", "");
			jQuery("#htmlcontent").val("");
			jQuery("#addtitle").attr("readonly",true);
			jQuery("#addshottitle").attr("readonly",true);
			
			var bean = date.bean;
			
			jQuery("#addid").val(bean.id);
			jQuery("#addtitle").val(bean.title);
			jQuery("#addshottitle").val(bean.shottitle);
			jQuery("#addtag").val(bean.tag);
			jQuery("#addauthor").val(bean.author);
			jQuery("#zhucolname").val(bean.zhucolumnname);
			jQuery("#zhucolnameid").val(bean.zhucolumnid);
			jQuery("#fucolname").val(bean.fucolumnname);
			jQuery("#fucolnameid").val(bean.fucolumnid);
			jQuery("#addsourceurl").val(bean.sourceurl);
			jQuery("#addsummary").val(bean.summary);
			//图片集合
			jQuery("#addimglist").val(date.imglist);
			//url集合
			jQuery("#addurlist").val(date.urlist);
			editor.html(bean.htmlcontent);
			//editor.text(bean.textcontent);
			jQuery("#microimgid").val(bean.imgid);
			jQuery("#img_show").attr("src", bean.imgsrc);
		}
	});
}
function deletefun(id){
	jQuery.messager.confirm("信息","您确定要删除此权限？",function(arg){
		if(!arg)
			return;
		jQuery.ajax({
			async:false,
			type:"post",
			dataType:"json",
			data:{id:id},
			url:"${ctx}/sys/archives/delete",
			success:function(date){
				if(!date.success){
					jQuery.messager.alert("信息",date.message,"error");
					return;
				}
				//刷新页面
				jQuery("#datagrid").treegrid("reload");
			}
		});
	});
}
<@archives.js />
</script>